<?php
namespace control;
/*
*待考试类
*/

class beTestedClass extends adIndexClass
{
    private const page = 'beTested';
    private const title = '待考试';

    /*列表-查询条件*/
    public function home()
    {
        //生成查询表单
        $html =
            text(array("name" => "title", "title" => L('person.table.sjbt'))) .
            text(array("name" => "createTime", 'class'=>'width200','laydate' => ['range' => '~'], "title" => L('person.table.kskssj'))) .
            select(array("name" => "state", "title" => L('person.table.zhuang_tai'), "option" => ['未开始','进行中','已结束']));
        //返回
        return $this->homeFun($this->search($html));
    }

    /*客户列表-查询结果*/
    public function homeSearch()
    {
        //like查询数组
        $like = array(
            "b.title" => $this->post('title'),//试卷标题
        );
        //全等查询数组
        $equal = array(
            "b.state" => '已发布',//状态
        );
        //过滤掉已交卷的试卷
        $and = " and a.stid = '{$this->control['stid']}'  and a.stid not in (select stid from topic_answer_record where paperId = b.id and isCommit = 1)";
        if ($this->post('createTime')) {
            $and .=$this->timeComparison($this->post('createTime'), field: 'startTime');
        }
        $and .= match ($this->post('state')) {
            '未开始' => "  and UNIX_TIMESTAMP(startTime) > UNIX_TIMESTAMP()",
            '进行中' => "  and UNIX_TIMESTAMP(startTime) <= UNIX_TIMESTAMP() and UNIX_TIMESTAMP(endTime) >= UNIX_TIMESTAMP()",
            '已结束' => " and UNIX_TIMESTAMP(b.endTime) < UNIX_TIMESTAMP()",
            default=>'',
        };

        //获取查询结果
        $where = search(array("like" => $like, "=" => $equal)) . $and;
        $page = $this->page(array("table" =>" paper_people as a join paper_library as b on a.orderId = b.id",'field'=>"b.*,a.stid as staffId ", "where" => $where, "order" => " a.time desc"));
        $tr[] = array(L('person.table.sjbt'), L('person.table.ti_mu_zong_shu'), L('person.table.zong_fen'), L('person.table.he_ge_fen_zhi'), L('person.table.kskssj'), L('person.table.ksjssj'), L('person.table.kszt'), L('person.table.cao_zuo'));
        //获取记录
        foreach ($page['array'] as $array) {
            $topicNum = static::total(array('table'=>'paper_library_detail','where'=>" and orderId = '$array[id]'"));
            $score = find(array('table'=>'paper_library_detail','sum'=>"score",'where'=>" and orderId = '$array[id]'"));
            if (time() >= strtotime($array['startTime']) && time() <= strtotime($array['endTime'])) {
                $state = '进行中';
            }elseif(time() > strtotime($array['endTime'])){
                $state = '已结束';
            } else {
                $state = '未开始';
            }
            if (strtotime($array['endTime'])<time()){
                $mx = "<span style='color: #e6e6e6'>".L('person.table.startTest')."</span>";
            }else{
                $mx = "<span class='del-row-button' onclick=\"window.open('" . root . "control/adIndex.html?cla=paperManagement&fun=previewPaper&method=answer&id={$array['id']}')\">".L('person.table.startTest')."</span>";
            }
            $tr[] = array($array['name'],$topicNum,$score,$array['passMark'], $array['startTime'], $array['endTime'],$state,$mx);
        }
        //控件
        $button = "";
        //返回
        $json['html'] = $page['hint'] . $button . "<form name='listForm'>" . space . $this->tableMany(array("tr" => $tr)) . "</form>" . $page['button'] . clear;
        return json_encode($json, 320);
    }

}

